package com.ganwei.mybatisplus.config;


import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.ganwei.mybatisplus.config.Interceptor.DataPermissionInterceptor;
import com.ganwei.mybatisplus.config.Interceptor.MySqlInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.ganwei.mybatisplus.system.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
@Order(-1)
@Import({MySqlInterceptor.class})
public class DataSourceConfig1 {

    @Autowired
    private MySqlInterceptor mySqlInterceptor;

    @Autowired
    private DataPermissionInterceptor dataPermissionInterceptor;

    @Primary // 表示这个数据源是默认数据源, 这个注解必须要加，因为不加的话spring将分不清楚那个为主数据源（默认数据源）
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1") //读取application.yml中的配置参数映射成为一个对象
    public DataSource getDb1DataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置，不然将报错：no statement （这种错误也可能是mapper的xml中，namespace与项目的路径不一致导致）
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/ganwei/mybatisplus/system/mapper/mapping/ds1/*.xml"));

        MybatisConfiguration configuration = new MybatisConfiguration();
        //多数据源下打印日志
        configuration.setLogImpl(StdOutImpl.class);
        //多数据源下拦截自定义的拦截器
        bean.setPlugins(new Interceptor[]{dataPermissionInterceptor});
        return bean.getObject();
    }

    @Primary
    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}